package com.watering.plan;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.StringTokenizer;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

public class CheckPlants extends Activity {
	TextView tv1 ;
	TextView tv2 ;
	TextView tv3 ;
	TextView tv4 ;
	TextView tv5 ;
	TextView tv1d ;
	TextView tv2d ;
	TextView tv3d;
	TextView tv4d ;
	TextView tv5d ;
	SeekBar sb1;
	SeekBar sb2;
	SeekBar sb3;
	SeekBar sb4;
	SeekBar sb5;
	Toast ts;
	Calendar cal;
	String plant1;
	String plant2;
	String plant5;
	String plant6;
	String plant7;
	
	
	public String getDay(String date){
		StringTokenizer st = new StringTokenizer(date,":");
		if(st.hasMoreTokens())
			return st.nextToken();
		else
			return "0";
	}
	
	public int getDiff(int nodays, String last){
		cal = Calendar.getInstance();
		int d = cal.DAY_OF_MONTH;
		int rez;
		
		StringTokenizer st = new StringTokenizer(last,": ");
		int day = Integer.parseInt(st.nextToken());
		
		rez = day - d;
		
		if( nodays - rez < 0 )
			return rez - nodays;
		else
			return nodays - rez;

	}
	
	public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
         setContentView(R.layout.checkplants);
         
         ts =  Toast.makeText(this, R.string.check_plant_help, Toast.LENGTH_SHORT);
         ts.setDuration(Toast.LENGTH_LONG);
         
         ImageView img = (ImageView)findViewById(R.id.helpbutton);
         img.setOnClickListener(new OnClickListener() {
	         public void onClick(View v) {
	        	ts.show();
	         }
	     });
         
         
         ImageView img2 = (ImageView)findViewById(R.id.grad);
         tv1d = new TextView(this);
         tv1d = (TextView)findViewById(R.id.details1);
         tv1 = new TextView(this);
         tv1 = (TextView)findViewById(R.id.check1);
         
         Base bs = new Base(this);
         String something = "";
         try{
				bs.createDataBase();
				bs.openDataBase();
				something = bs.getReg();
				bs.close();
		}catch(Exception e){
		}
		 if( something.contains("nimic"))
			 something = "";
         
         tv2 = new TextView(this);
         tv2 = (TextView)findViewById(R.id.check2);
         tv2d = new TextView(this);
         tv2d = (TextView)findViewById(R.id.details2);
         tv2d.setOnClickListener(new OnClickListener() {
        	 public void onClick(View v) {
        		 Intent myIntent = new Intent( CheckPlants.this, ShowPlant.class);
	        	 myIntent.putExtra("plantname", tv2.getText() );
		     	 startActivity(myIntent);
        	 }});
         
         tv3 = new TextView(this);
         tv3 = (TextView)findViewById(R.id.check3);
         tv3d = new TextView(this);
         tv3d = (TextView)findViewById(R.id.details3);
         tv3d.setOnClickListener(new OnClickListener() {
        	 public void onClick(View v) {
        		 Intent myIntent = new Intent( CheckPlants.this, ShowPlant.class);
	        	 myIntent.putExtra("plantname", tv3.getText() );
		     	 startActivity(myIntent);
        	 }});
         
         tv4 = new TextView(this);
         tv4 = (TextView)findViewById(R.id.check4);
         tv4d = new TextView(this);
         tv4d = (TextView)findViewById(R.id.details4);
         tv4d.setOnClickListener(new OnClickListener() {
        	 public void onClick(View v) {
        		 Intent myIntent = new Intent( CheckPlants.this, ShowPlant.class);
	       	     myIntent.putExtra("plantname", tv4.getText() );
		     	 startActivity(myIntent);
        	 }});
         
         tv5 = new TextView(this);
         tv5 = (TextView)findViewById(R.id.check5);
         tv5d = new TextView(this);
         tv5d = (TextView)findViewById(R.id.details5);
         tv5d.setOnClickListener(new OnClickListener() {
        	 public void onClick(View v) {
        	//	 Intent myIntent = new Intent( CheckPlants.this, ShowPlant.class);
	        //	 myIntent.putExtra("plantname", tv5.getText() );
		    // 	 startActivity(myIntent);
        	 }});
         
         sb1 = new SeekBar(this);
         sb1 = (SeekBar)findViewById(R.id.SeekBar01);
         sb1.setEnabled(false);
        
         
         sb2 = new SeekBar(this);   
         sb2 = (SeekBar)findViewById(R.id.SeekBar02);
         sb2.setEnabled(false);
        
         
         sb3 = new SeekBar(this);
         sb3 = (SeekBar)findViewById(R.id.SeekBar03);
         sb3.setEnabled(false);
         
         sb4 = new SeekBar(this);
         sb4 = (SeekBar)findViewById(R.id.SeekBar04);
         sb4.setEnabled(false);
         
         
         sb5 = new SeekBar(this);
         sb5 = (SeekBar)findViewById(R.id.SeekBar05);
         sb5.setEnabled(false);
         
 
 		StringTokenizer st = new StringTokenizer(something,";");
 		if( st.hasMoreTokens() ){
 			StringTokenizer st1 = new StringTokenizer(st.nextToken(),",:");
 			tv1.setVisibility(View.VISIBLE);
 			tv1d.setVisibility(View.VISIBLE);
 			plant1 = st1.nextToken();
 			tv1.setText(plant1);
 			String nodays = st1.nextToken();
 			sb1.setMax(Integer.parseInt(nodays));
 			//sb1.setProgress(2);
 		//	sb2.setProgress(Integer.parseInt(nodays)- 1);
 			sb1.setProgress(getDiff(Integer.parseInt(nodays),st1.nextToken()));
 			
 			tv1d.setOnClickListener(new OnClickListener() {
 		       	 public void onClick(View v) {
 		       		 Intent myIntent = new Intent( CheckPlants.this, ShowPlant.class);
 			        	 myIntent.putExtra("plantname", plant1 );
 				    	 startActivity(myIntent);
 		       	 }});
 		}
 		
 		if( st.hasMoreTokens() ){
 			StringTokenizer st2 = new StringTokenizer(st.nextToken(),",:");
 			tv2.setVisibility(View.VISIBLE);
 			tv2d.setVisibility(View.VISIBLE);
 			sb2.setVisibility(View.VISIBLE);
 			tv2.setText(st2.nextToken());
 			String nodays = st2.nextToken();
 			sb2.setMax(Integer.parseInt(nodays));
 			int dif = getDiff(Integer.parseInt(nodays),st2.nextToken());
 			sb2.setProgress(dif);
 		//	bs.addNotify(dif);
 		}
 		
 		if( st.hasMoreTokens() ){
 			StringTokenizer st3 = new StringTokenizer(st.nextToken(),",:");
 			tv3.setVisibility(View.VISIBLE);
 			tv3d.setVisibility(View.VISIBLE);
 			tv3.setText(st3.nextToken());
 			sb3.setVisibility(View.VISIBLE);
 			String nodays = st3.nextToken();
 			sb3.setMax(Integer.parseInt(nodays));
 			int diff= getDiff(Integer.parseInt(nodays),st3.nextToken());
 			sb3.setProgress(diff);
 		//	bs.addNotify(diff);
 		}
 		
 		if( st.hasMoreTokens() ){
 			StringTokenizer st4 = new StringTokenizer(st.nextToken(),",:");
 			tv4.setVisibility(View.VISIBLE);
 			tv4.setText(st4.nextToken());
 			tv4d.setVisibility(View.VISIBLE);
 			sb4.setVisibility(View.VISIBLE);
 			String nodays = st4.nextToken();
 			sb4.setMax(Integer.parseInt(nodays));
 			sb4.setProgress(getDiff(Integer.parseInt(nodays),st4.nextToken()));
 		}
 		if( st.hasMoreTokens() ){
 			StringTokenizer st5 = new StringTokenizer(st.nextToken(),",:");
 			tv5.setVisibility(View.VISIBLE);
 			tv5d.setVisibility(View.VISIBLE);
 			tv5.setText(st5.nextToken());
 			sb5.setVisibility(View.VISIBLE);
 			String nodays = st5.nextToken();
 			sb5.setMax(Integer.parseInt(nodays));
 			sb5.setProgress(getDiff(Integer.parseInt(nodays),st5.nextToken()));
 		}
 		
 		
		
	}  
}